﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;

namespace TriviaViewModels
{
    public class WizardViewBase:ViewModelBase
    {
        private int numberOfShowAboutClicks = 0;


        private static GameViewModelWizard _gameViewModel;
        private static ScoreBoardViewModelWizard _scoreBoardViewModel;
        private static EntryViewModelWizard _entranceViewModel;

        private Visibility _isAboutLableShown;
        public Visibility IsAboutLableShown
        {
            get { return _isAboutLableShown; }
            set
            {
                if (_isAboutLableShown != value)
                {
                    _isAboutLableShown = value;
                    RaisePropertyChanged("IsAboutLableShown");
                }
            }
        }

        private ViewModelBase _currentViewModel;
        public ViewModelBase CurrentViewModel
        {
            get { return _currentViewModel; }
            set
            {
                if (_currentViewModel != value)
                {
                    _currentViewModel = value;
                    RaisePropertyChanged("CurrentViewModel");
                }
            }
        }

        public ICommand GoToGameCommand { get; set; }
        public ICommand GoToScoreBoardCommand { get; set; }
        public ICommand GoToMainWindowCommand { get; set; }
        public ICommand ShowAboutGameCommand { get; set; }

        public WizardViewBase()
        {
            IsAboutLableShown = Visibility.Collapsed;
            if (IsInDesignMode) return;
            WizardViewBase._entranceViewModel = new EntryViewModelWizard();
            CurrentViewModel = WizardViewBase._entranceViewModel;
            GoToGameCommand = new RelayCommand(() => ExecuteGoToGameCommand());
            GoToScoreBoardCommand = new RelayCommand(() => ExecuteGoToScoreBoardCommand());
            GoToMainWindowCommand = new RelayCommand(() => ExecuteGoToStartPageCommand());
            ShowAboutGameCommand = new RelayCommand(() => ExecteShowAboutGameCommand());
        }

        private void ExecuteGoToGameCommand()
        {
            var difficulty = WizardViewBase._entranceViewModel.SelectedDifficulty;
            var topic = WizardViewBase._entranceViewModel.SelectedTopic;
            var name = WizardViewBase._entranceViewModel.UserName;
            var password = WizardViewBase._entranceViewModel.Password;
            if (difficulty != null && topic != null)
            {
                 WizardViewBase._gameViewModel=new GameViewModelWizard();
                  CurrentViewModel = WizardViewBase._gameViewModel;
                if (WizardViewBase._entranceViewModel.UserName != null && WizardViewBase._entranceViewModel.Password != null)
                    WizardViewBase._gameViewModel.SetUser(WizardViewBase._entranceViewModel.UserName, WizardViewBase._entranceViewModel.Password);
                 WizardViewBase._gameViewModel.SetQuestionsAndAnswers(difficulty,topic);
            }
            else
                System.Windows.Forms.MessageBox.Show("Choose difficulty and topic"); 
        }

        private void ExecuteGoToScoreBoardCommand()
        {
            WizardViewBase._scoreBoardViewModel = new ScoreBoardViewModelWizard();
            CurrentViewModel = WizardViewBase._scoreBoardViewModel;
        }

        private void ExecuteGoToStartPageCommand()
        {
            WizardViewBase._entranceViewModel = new EntryViewModelWizard();
            CurrentViewModel = _entranceViewModel;
        }

        private void ExecteShowAboutGameCommand()
        {
            numberOfShowAboutClicks++;
            if (numberOfShowAboutClicks % 2 == 0)
                IsAboutLableShown = Visibility.Collapsed;
            else
                IsAboutLableShown = Visibility.Visible;
        }
    }
    
}
